package main

import (
	"fmt"
	"github.com/veandco/go-sdl2/sdl"
)

const (
	W = 670
	H = 620
)

var (
	Gwindow     *sdl.Window
	Grender     *sdl.Renderer
	backTexture *Texture
	upTexture   *Texture
)

func init() {
	sdl.Init(sdl.INIT_EVERYTHING)
	Gwindow, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, W, H, sdl.WINDOW_SHOWN)
	if err != nil {
		panic(err)
	}
	Grender, err = sdl.CreateRenderer(Gwindow, -1, sdl.RENDERER_ACCELERATED)
	if err != nil {
		panic(err)
	}
	Grender.SetDrawColor(128, 128, 128, 255)
	backTexture, upTexture = &Texture{}, &Texture{}
	backTexture.LoadFromFile("../asserts/image/fadein.png")
	upTexture.LoadFromFile("../asserts/image/fadeout.png")
	upTexture.SetBlendMode(sdl.BLENDMODE_BLEND)
}

func main() {
	a := 255

	running := true
	for running {
		for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch t := event.(type) {
			case *sdl.QuitEvent:
				println("Quit")
				running = false
				break
			//case *sdl.KeyboardEvent:
			case *sdl.KeyboardEvent:
				if t.State == sdl.PRESSED {
					switch t.Keysym.Sym {
					case sdl.K_q:
						if a+32 > 255 {
							a = 255
						} else {
							a += 32
						}
						fmt.Println("alpha = ", a)
					case sdl.K_w:
						if a-32 < 0 {
							a = 0
						} else {
							a -= 32
						}
					}
				}
			}
		}
		Grender.Clear()
		backTexture.Render(0, 0, nil)
		upTexture.SetAlpha(uint8(a))
		upTexture.Render(0, 0, nil)

		Grender.Present()
		sdl.Delay(16)
	}
	free()
}

func free() {
	backTexture.Free()
	upTexture.Free()
	Grender.Destroy()
	Gwindow.Destroy()
	sdl.Quit()
}
